function summary_tc(specificones)
% summary timecourses for ROIs

if nargin < 1
    specificones = [];
end
load_var
[xls_num,xls_txt] = xlsread('\\p1-372\data_D\Stacks.xls');%xlsread(evalin('base', 'vars.path.batch.file'));
adata_dir = '\\p1-372\data_D\data\AnalyzedData\'; %evalin('base', 'vars.path.Adata');
stats_dir = '\\p1-372\data_D\data\AnalyzedData\StatFiles\';
output_dir = stats_dir;

s = filesep;
hue_table = makeHueTable(100,60/360); % generate color lookup table


for fnd = 1:size(xls_num,1)
    goforit = 0;
    if isempty(specificones) && xls_num(fnd,8) == xls_num(fnd,5) &&  xls_num(fnd,11) == 1
        goforit = 1;
    elseif ~isempty(specificones) && xls_num(fnd,8) == xls_num(fnd,5) && sum(find(specificones == xls_num(fnd,5))) == 1
        goforit = 1;
    end
    if goforit% new stack and starting point

        animal = cell2mat(xls_txt(fnd+1,9));
        dateid = datestr(datenum(xls_txt(fnd+1,2),'dd.mm.yyyy'),'yyyy-mm-dd');
        disp(['Processing ' num2str(xls_num(fnd,5)) ' ...'])
        load([adata_dir animal s dateid s animal '-Adata-' num2str(xls_num(fnd,5)) '.mat']) % load the first Adata file
        % determine all associated files
        series = find(xls_num(:,8) == xls_num(fnd,5));
%         series = series(xls_num(series,5) ~= xls_num(fnd,5)); % take out ref image
        series = series(xls_num(series,11) == 1); % take out all we dont use
%         series = [xls_num(fnd,5) series];  % put start in front
        outputfilename = ['tc_' animal '#' num2str(xls_num(fnd,5)) '.ps'];
        
        stepsize = 1 / length(series);
        if length(series) > 2 && ~isempty(whos('ROIs'))
            npages = ceil(length(ROIs)/8);
            
            for ind = 1:length(series)
                dateid2 = datestr(datenum(xls_txt(series(ind)+1,2),'dd.mm.yyyy'),'yyyy-mm-dd');
                if ind>1
                    % load Adata file
                    load([adata_dir animal s dateid2 s animal '-Adata-' num2str(xls_num(series(ind),5)) '.mat']);
                end
                %load stats file
                statsfile = dir([stats_dir '*#' num2str(xls_num(series(ind),5)) '.mat']);
                load([stats_dir statsfile.name]);
                results(ind).ROIs = ROIs;
                correctframes = [correct_wrong_frames(dx') correct_wrong_frames(dy')];
                correctframes = unique(correctframes);
                if correctofames(1) ==1
                    correctframes = correctframes(2:end);
                end
                
                results(ind).tmask = zeros(size(template));
                for rnd = 1:length(ROIs)
                    results(ind).tmask(ROIs(rnd).indices) = rnd;
                    results(ind).ROIs(rnd).activity(correctframes) = results(ind).ROIs(rnd).activity(correctframes - 1);
                    results(ind).ROIs(rnd).activity = psmooth(results(ind).ROIs(rnd).activity) / ...
                        median(results(ind).ROIs(rnd).activity);
                    results(ind).ROIs(rnd).min = min(results(ind).ROIs(rnd).activity);
                    results(ind).ROIs(rnd).max = max(results(ind).ROIs(rnd).activity);
                end
                results(ind).template = template-min(min(template));
                results(ind).template = double(results(ind).template)/max(max(results(ind).template));
                results(ind).imROI(:,:,1) = max(results(ind).template,bwperim(results(ind).tmask));
                results(ind).imROI(:,:,2) = results(ind).template;
                results(ind).imROI(:,:,3) = results(ind).template;
               
                results(ind).ids = mapsref.ids;
                clear ROIs mapsref
                
                for mnd=1:length(results(ind).ROIs)
                    results(ind).pvalues_sel_mov(mnd) = RespStats(mnd).movResp.p_value_sel;
                    results(ind).pvalues_sel_stand(mnd) = RespStats(mnd).standResp.p_value_sel;
                    results(ind).ori_vector_angle_mov(mnd) = RespStats(mnd).movResp.ori_vector_angle;
                    results(ind).ori_vector_angle_stand(mnd) = RespStats(mnd).standResp.ori_vector_angle;
                end
                results(ind).coloredCellMap_mov = MakeCellMap(results(ind).ori_vector_angle_mov/180, 'all', results(ind).tmask, results(ind).template, hue_table);
                results(ind).coloredCellMap_stand = MakeCellMap(results(ind).ori_vector_angle_stand/180, 'all', results(ind).tmask, results(ind).template, hue_table);
                
            end
            text_pos1 = fix(length(results(ond).ids.stim_on)/size(results(1).ids.stim_boundaries,2)*0.96)
            for pnd=1:npages
                fig = figure(1010);
                % set(fig,'position',[100 100 1414 1000]);
                set(fig,'PaperOrientation', 'landscape', 'PaperType', 'A4', 'PaperPositionMode','auto');
                set(fig,'PaperPosition',[0.5 0.5 27.6 19.5]);
                for ond=1:length(series)
                    axes('Position',[0+(ond-1)*stepsize 1-2/11 stepsize 2/11]);
                    image(results(ond).imROI);axis off
                    text(580,20,num2str(xls_num(series(ond),5)),'color',[1 1 1]*0.75,'fontsize',5,'fontweight','bold');
                    
                    %label cells
                    for mnd=1:length(results(ond).ROIs)
                        [txt_x,txt_y]=ind2sub(size(template),min(results(ond).ROIs(mnd).indices));
                        text(txt_y-10,txt_x-10,num2str(mnd),'color',[1 1 1]*0.75,'fontsize',5,'fontweight','bold')
                    end
                    
                    axes('Position',[0+(ond-1)*stepsize 1-3/11 0.5*stepsize 1/11]);
                    image(results(ond).coloredCellMap_mov);axis off
                    text(580,20,'mov','color',[1 1 1]*0.75,'fontsize',3,'fontweight','bold');

                    axes('Position',[0+(ond-1)*stepsize+0.5*stepsize 1-3/11 0.5*stepsize 1/11]);
                    image(results(ond).coloredCellMap_stand); axis off;
                    text(580,20,'stand','color',[1 1 1]*0.75,'fontsize',3,'fontweight','bold');
                    
                    if pnd == npages
                        ROIend = min(length(results(ond).ROIs),pnd*8);
                    else
                        ROIend = 8;
                    end
                    % timecourses
                    
                    for mnd = ((pnd-1)*8)+1:ROIend
                        axes('Position',[0.01+(ond-1)*stepsize 1-(mnd-((pnd-1)*8)+3)/11-0.015 stepsize-0.02 1/11-0.015]);
                        lim_y_min=[];lim_y_max=[];
                        for qnd = 1:length(series)
                            lim_y_min(qnd) = results(qnd).ROIs(mnd).min;
                            lim_y_max(qnd) = results(qnd).ROIs(mnd).max;
                        end
                        plotOri(results(ond).ROIs(mnd).activity, results(ond).ids, ... 
                            'newfigurehandle',0,'fontsize',5,'lim_y', [min(lim_y_min) max(lim_y_max)], 'x_label',0);
                        title(['cell #' num2str(mnd) '   pvalue_m sel: ' num2str(results(ond).pvalues_sel_mov(mnd)) ...
                            '   pvalue_s sel: ' num2str(results(ond).pvalues_sel_stand(mnd))], 'fontsize',5,'fontweight','bold');
                    end
                        
                end
                if pnd == 1
                    print('-dpsc',[output_dir outputfilename]);
                else
                    print('-dpsc',[output_dir outputfilename],'-append');
                end
                close 1010 % close figure
            end
%             dos(['"' vars.path.acrobat '\acrodist ' output_dir outputfilename '"']);
             
        end
    end
end

